#男性:BMR(Kcal)=66.5+13.8x体重(kg)+5.0x身高(cm)-6.8x年龄(岁)
#女性:BMR(Kcal)=665.1+9.6x体重(kq)+1.8x身高(cm)-4.7x年龄(岁)
import csv

def menu():
    print("欢迎使用BMI计算器！")
    print("请选择操作:")
    print("1.进入BMR计算器")
    print("2.退出程序")
    print("3.生成储存BMR计算数据的CSV文件表头")
    choice = input("请输入选项:")
    return int(choice)

def count():
    gender = input("请输入性别(男/女):")
    age = int(input("请输入年龄(岁):"))
    height = float(input("请输入身高(cm):"))
    weight = float(input("请输入体重(kg):"))


    if gender == '女':
        bmr = 665.1 + 9.6 * weight + 1.8 * height - 4.7 * age
    elif gender == '男':
        bmr = 66.5 + 13.8 * weight + 5.0 * height - 6.8 * age
    else:
        print("性别输入错误")
        return

    print("您的BMR指数为：", bmr)
    #将数据写入CSV文件
    save_choice = input("是否保存本次记录? (Y/N): ")
    if save_choice == "Y":
        with open("bmr_records.csv", "a", newline="") as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerow([gender, age, height, weight, bmr])

def generate_csv_file():
    with open("bmr_records.csv", "a", newline="") as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(["性别", "年龄", "身高", "体重", "BMR"])
    print("CSV文件表头已生成。")

def main():
    while True:
        choice = menu()
        if choice == 1:
            count()
        elif choice == 2:
            print("程序已退出!")
            break
        elif choice == 3:
            generate_csv_file()
        else:
            print("输入错误，请重新输入。")

        continue_program = input("是否继续程序?(是/否)")
        if continue_program == "是":
            break
if __name__ == '__main__':
     main()